import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * @Author:Star
 * @Date:Created in 18:54 2019/5/11
 * @Description:
 */
class Menu {
    Scanner input = new Scanner(System.in);
    private KFCFactoryImpl kfcFactory;
    private Customer customer;
    private float hamburgMoney;
    private float iceCreamMoney;
    private float beverageMoney;
    private float chickenFiresMoney;
    private float setMealMoney;
    private float sumMoney;

    public Menu(KFCFactoryImpl kfcFactory, Customer customer) {
        this.kfcFactory = kfcFactory;
        this.customer = customer;
    }

    //欢迎界面
    public void menu() throws IOException {
        System.out.println("******欢迎光临荔枝堡***");
        System.out.println("    ***          ***   ");
        System.out.println("  ********    ********  ");
        System.out.println("***         *        ***");
        System.out.println("  ***  1.开始点餐   *** ");
        System.out.println("    ** 2.退出系统  **   ");
        System.out.println("      **         **    ");
        System.out.println("         **   **"       );
        System.out.println("            **         ");
        System.out.print("请选择>");
        int choose = input.nextInt();
        switch (choose){
            case 1:
                productionMenu();
                int pay = payMoney(customer,getMoney());
                printBill(customer,getMoney(),pay);
                break;
            case 2:
                System.out.println("您已成功退出点餐系统...");
                default:
                    System.out.println("选择错误....");
        }
    }

    //食物产品界面
    public void productionMenu() throws IOException {
        boolean flag = true;
        while(flag == true){
            System.out.println("当前商品如下：");
            System.out.println("==================");
            System.out.println("1.汉堡");
            System.out.println("==================");
            System.out.println("2.冰淇淋");
            System.out.println("==================");
            System.out.println("3.饮料");
            System.out.println("==================");
            System.out.println("4.烤鸡");
            System.out.println("==================");
            System.out.println("5.套餐");
            System.out.println("==================");
            System.out.println("6.结束订餐，查看订单");
            System.out.println("==================");
            System.out.print("请选择>");
            int choose = input.nextInt();
            switch (choose){
                case 1:
                    hamburgMenu();
                    break;
                case 2:
                    iceCreamMenu();
                    break;
                case 3:
                    beverageMenu();
                    break;
                case 4:
                    chickenFiresMenu();
                    break;
                case 5:
                    setMeal();
                    break;
                case 6:
                    float money = getMoney();
                    showBills(customer,money);
                    break;
                default:
                    System.out.println("选择错误...");
            }
            if(choose == 6){
                flag = false;
            }
        }

    }

    //汉堡菜单界面
    public void hamburgMenu(){
        System.out.println("1.巨无霸-----------25元");
        System.out.println("2.香辣鸡腿堡--------18元");
        System.out.println("3.奥尔良鸡腿堡------18元");
        System.out.print("请选择>");
        int choose = input.nextInt();
        System.out.print("请输入数量>");
        int num = input.nextInt();
        hamburgMoney = customer.getHamburg(choose,num);
    }

    //冰淇淋菜单界面
    public void iceCreamMenu(){
        System.out.println("1.草莓圣代------12元");
        System.out.println("2.蓝莓圣代------12元");
        System.out.println("3.  甜筒--------6元");
        System.out.print("请选择>");
        int choose = input.nextInt();
        System.out.print("请输入数量>");
        int num = input.nextInt();
        iceCreamMoney = customer.getIceCream(choose,num);
    }

    //饮料菜单界面
    public void beverageMenu(){
        System.out.println("1.可口可乐------12元");
        System.out.println("2.九珍果汁------11元");
        System.out.print("请选择>");
        int choose = input.nextInt();
        System.out.print("请输入数量>");
        int num = input.nextInt();
        beverageMoney = customer.getBeverage(choose,num);
    }

    //炸鸡菜单界面
    public void chickenFiresMenu(){
        System.out.println("1.上校鸡块------12元");
        System.out.println("2. 鸡肉卷 ------10元");
        System.out.println("3. 鸡米花 ------8元");
        System.out.print("请选择>");
        int choose = input.nextInt();
        System.out.print("请输入数量>");
        int num = input.nextInt();
        chickenFiresMoney = customer.getChicken(choose,num);
    }

    //套餐界面
    public void setMeal(){
        System.out.println("1.儿童套餐（奥尔良鸡腿堡+九珍果汁+上校鸡块)------35元");
        System.out.println("2.家庭套餐（香辣鸡腿堡（3个）+九珍果汁（3杯）+鸡肉卷（2个）+草莓圣代)------118元");
        System.out.println("3.情侣套餐（巨无霸（2个）+甜筒（2个）+九珍果汁(2杯)+上校鸡块)------99元");
        System.out.print("请选择>");
        int choose = input.nextInt();
        System.out.print("请输入数量>");
        int num = input.nextInt();
        setMealMoney = customer.getSetMeal(choose,num);
    }

    //获得总金额
    public float getMoney(){
        sumMoney = hamburgMoney+iceCreamMoney+beverageMoney+chickenFiresMoney+setMealMoney;
        return sumMoney;
    }

    //打印账单
    public static void printBill(Customer customer, float sumMoney, int pay) throws IOException {
        customer.showBill();
        BufferedWriter bw=new BufferedWriter(new FileWriter("D://text.txt",true));
        bw.write("总计: "+sumMoney);
        bw.newLine();
        bw.write("付款："+pay);
        bw.newLine();
        float y=pay-sumMoney;
        bw.write("找零："+y);
        bw.newLine();
        bw.write("当前收银员：小荔子\r\n");
        bw.write("                         欢迎下次光临！^_^                        \r\n");
        bw.write("******************************************************\r\n");
        bw.flush();
        bw.close();
    }

    //显示订单
    public static void showBills(Customer customer,float sumMoney) throws IOException {
        customer.outBill();
        System.out.println("总计："+sumMoney);
    }

    //付款界面
    public int payMoney(Customer customer, float sumMoney){
        while(true){
            System.out.print("请输入付款金额:");
            int pay=input.nextInt();
            if(pay-sumMoney >= 0){
                if(pay >= 100){
                    System.out.println("您要使用优惠卷结账吗？（y/n）");
                    System.out.println("优惠卷：满100减20，满200减30！！！");
                    if(input.next().equals("y")){
                        if(pay >= 200){
                            System.out.println("找零:"+(pay-sumMoney+30));
                        }else {
                            System.out.println("找零:"+(pay-sumMoney+20));
                        }
                    }else{
                        System.out.println("找零:"+(pay-sumMoney));
                    }
                }else{
                    System.out.println("找零:"+(pay-sumMoney));
                }
            }else{
                System.out.println("不够哦!!!");
            }
            return pay;
        }
    }
}